/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.table;

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import simplemetrosystem.control.EditCommandManager;
import simplemetrosystem.control.ElementManager;
import simplemetrosystem.model.Element.MetroLine;
import simplemetrosystem.model.Element.MetroStation;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.InformationPane;
import simplemetrosystem.view.table.Exceptions.InformationPanelException;

/**
 *
 * @author Eric Qian
 */
public class AllStationTable extends JTable {

    /**
     * 
     * @param infPane 
     */
    public AllStationTable(InformationPane infPane) {
        super();
        this.infPane = infPane;
        allStationModel = new AllStationModel(infPane);
        setModel(allStationModel);
        setRowSelectionAllowed(false);
        setAutoResizeMode(AllLineTable.AUTO_RESIZE_OFF);
        getTableHeader().setReorderingAllowed(false);
        getTableHeader().setResizingAllowed(false);

        setCell();

        setRowHeight(ROW_HEI);

        for (int i = 0; i < getColumnCount(); i++) {
            int with = getPreferredWidthForCloumn(this, i) + 10;
            with = MIN_WIDTH > with ? MIN_WIDTH : with;
            this.getColumnModel().getColumn(i).setPreferredWidth(with);
        }

    }

    private void setCell() {
        TableColumn numColumn = columnModel.getColumn(AllStationModel.COL_NUM);
        numColumn.setCellEditor(new AllStationNumCellEditor(infPane));
    //ListSelectionModel rowSM = getSelectionModel();
    //rowSM.addListSelectionListener(new StationListSelectionListener(infPane));
    }

    private int getPreferredWidthForCloumn(JTable table, int icol) {

        TableColumnModel tcl = table.getColumnModel();
        TableColumn col = tcl.getColumn(icol);
        int c = col.getModelIndex();
        int width = 0;
        int maxw = 0;

        for (int r = 0; r < table.getRowCount(); ++r) {
            TableCellRenderer renderer = table.getCellRenderer(r, c);
            Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, c), false, false, r, c);
            width = comp.getPreferredSize().width;
            maxw = width > maxw ? width : maxw;
        }
        return maxw < MAX_WIDTH ? maxw : MAX_WIDTH;
    }
    private InformationPane infPane;
    private AllStationModel allStationModel;
    public static final int MIN_WIDTH = 75;
    public static final int MAX_WIDTH = 125;
    public static final int ROW_HEI = 50;
}

class AllStationModel extends AbstractTableModel {

    /**
     * 
     * @param frame
     * @param elementManager
     */
    public AllStationModel(InformationPane infPane) {
        this.infPane = infPane;
        elementManager = ElementManager.getInstance();
        nameMap = NameMap.getInstance();
        editCommandManager = EditCommandManager.getInstance();
        String[] Tcolumn = {"", nameMap.getName("English Name"), nameMap.getName("Chinese Name"),
            nameMap.getName("Line Name"), nameMap.getName("Logic X"), nameMap.getName("Logic Y"),
            nameMap.getName("Natual X"), nameMap.getName("Natual Y"), nameMap.getName("Edges Number")
        };
        columnNames = Tcolumn;
    }

    public int getRowCount() {
        return elementManager.getStationsSize();
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex == COL_NUM || columnIndex == COL_NAM ||
                columnIndex == COL_CNAM || columnIndex == COL_NATX || columnIndex == COL_NATY) {
            return true;
        }
        return false;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        MetroStation aStation = elementManager.getStationAt(rowIndex);
        if (columnIndex == COL_NAM) {
            editCommandManager.EditStation(aStation, (String) aValue,
                    aStation.getChineseName(), aStation.getLogX(),
                    aStation.getLogY(), aStation.getNatX(), aStation.getNatY());
        } else if (columnIndex == COL_CNAM) {
            editCommandManager.EditStation(aStation, aStation.getName(),
                    (String) aValue, aStation.getLogX(),
                    aStation.getLogY(), aStation.getNatX(), aStation.getNatY());
        } else if (columnIndex == COL_NATX) {
            editCommandManager.EditStation(aStation, aStation.getName(),
                    aStation.getChineseName(), aStation.getLogX(),
                    aStation.getLogY(), Double.parseDouble((String) aValue), aStation.getNatY());
        } else if (columnIndex == COL_NATY) {
            editCommandManager.EditStation(aStation, aStation.getName(),
                    aStation.getChineseName(), aStation.getLogX(),
                    aStation.getLogY(), aStation.getNatX(), Double.parseDouble((String) aValue));
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == COL_NUM) {
            return rowIndex + 1;
        } else if (columnIndex == COL_NAM) {
            return elementManager.getStationAt(rowIndex).getName();
        } else if (columnIndex == COL_CNAM) {
            return elementManager.getStationAt(rowIndex).getChineseName();
        } else if (columnIndex == COL_LINES) {
            String output = "";
            MetroStation aStation = elementManager.getStationAt(rowIndex);
            for (int i = 0; i < aStation.getLinesSize(); i++) {
                output += aStation.getLineAt(i).getDisplayName() + ",";
            }
            return output;
        } else if (columnIndex == COL_LOGX) {
            return elementManager.getStationAt(rowIndex).getLogX();
        } else if (columnIndex == COL_LOGY) {
            return elementManager.getStationAt(rowIndex).getLogY();
        } else if (columnIndex == COL_NATX) {
            return elementManager.getStationAt(rowIndex).getNatX();
        } else if (columnIndex == COL_NATY) {
            return elementManager.getStationAt(rowIndex).getNatY();
        } else if (columnIndex == COL_EDGE_SIZE) {
            return elementManager.getStationAt(rowIndex).getEdgesSize();
        } else {
            return "???";
        }
    }
    private InformationPane infPane;
    private NameMap nameMap;
    private ElementManager elementManager;
    private EditCommandManager editCommandManager;
    public static final int COL_NUM = 0;
    public static final int COL_NAM = 1;
    public static final int COL_CNAM = 2;
    public static final int COL_LINES = 3;
    public static final int COL_LOGX = 4;
    public static final int COL_LOGY = 5;
    public static final int COL_NATX = 6;
    public static final int COL_NATY = 7;
    public static final int COL_EDGE_SIZE = 8;
    private String[] columnNames;
}

class AllStationNumCellEditor extends DefaultCellEditor {

    public AllStationNumCellEditor(InformationPane infPane) {
        super(new JTextField());
        this.infPane = infPane;
        elementManager = ElementManager.getInstance();
    }

    @Override
    public Object getCellEditorValue() {
        return null;
    }

    @Override
    public Component getTableCellEditorComponent(
            JTable table, Object value, boolean isSelected, int row, int column) {
        try {
                infPane.setFocus(InformationPane.STATION, elementManager.getStationAt(row));
        } catch (InformationPanelException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    private InformationPane infPane;
    private ElementManager elementManager;
}
/**
class StationListSelectionListener implements ListSelectionListener {

public StationListSelectionListener(InformationPane infPane) {
this.infPane = infPane;
elementManager = ElementManager.getInstance();
}

public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == true) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
int selectedRow = lsm.getMinSelectionIndex();
if (selectedRow != 0) {
infPane.setHighLight(elementManager.getStationAt(selectedRow - 1));
infPane.getAllStationTablePane().
setStation(elementManager.getStationAt(selectedRow - 1));
}
}
}
private InformationPane infPane;
private ElementManager elementManager;
}
 */
